package com.shoujifeng.snowmusic.player.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.shoujifeng.snowmusic.db.DatabaseUtil;
import com.shoujifeng.snowmusic.entity.Mp3Info;
import com.shoujifeng.snowmusic.player.MusicByPlayerActivity;
import com.shoujifeng.snowmusic.player.MusicMenuContentActivity;
import com.shoujifeng.snowmusic.player.MusicPlayerActivity;
import com.shoujifeng.snowmusic.player.NativeMusicManageActivity;
import com.shoujifeng.snowmusic.player.LrcProcess.LrcContent;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.provider.MediaStore;

public class MusicPlayer {
	public Context context;
	public static int MUSCI_MSG = 0; // 1 閿熸枻鎷烽敓鏂ゆ嫹 2 閿熸枻鎷峰仠 3 鍋滄 4 閿熸枻鎷烽敓鏂ゆ嫹閿燂拷5 閿熸枻鎷蜂竴閿熸枻鎷�
										// 6 閿熸枻鎷蜂竴閿熸枻鎷�
	public static int PLAY_ORDER = 0; // 0 椤洪敓鏂ゆ嫹 1 閿熸枻鎷烽敓锟�3 閿熸枻鎷烽敓鏂ゆ嫹
	public static boolean isPlaying = true; // 閿熸枻鎷烽敓鑺傝鎷烽敓鏂ゆ嫹
	private boolean isPause; // 閿熸枻鎷峰仠s
 
	public static List<Mp3Info> musicInfos;
	public static int position;
	public static List<LrcContent> LrcContent = null;

	public MusicPlayer(Context context) {
		this.context = context;
	}

	/**
	 * 
	 * 
	 * @return
	 */
	public List<Mp3Info> getMp3Infos(Context context) {
		List<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
		if (MusicMenuContentActivity.formActivity
				.equals("MusicMenuContentActivity")) {
			DatabaseUtil databaseUtil = new DatabaseUtil(context);
			List<HashMap<String, Object>> list = databaseUtil
					.queryMusicMenuList(NativeMusicManageActivity.musicName);
			for (int i = 0; i < list.size(); i++) {

				Mp3Info mp3Info = new Mp3Info();
				HashMap<String, Object> map = list.get(i);
				String title = map.get("musicName").toString(); // 
				String artist = map.get("playerName").toString(); // 
				long duration = Long.parseLong(map.get("duration").toString()); 
				long size = Long.parseLong(map.get("size").toString()); 
				String url = map.get("url").toString(); 

				mp3Info.setTitle(title);
				mp3Info.setArtist(artist);
				mp3Info.setDuration(duration);
				mp3Info.setSize(size);
				mp3Info.setUrl(url);
				mp3Infos.add(mp3Info);
			}
			return mp3Infos;
		} else {
			Cursor cursor = context.getContentResolver().query(
					MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
					null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToNext();
				Mp3Info mp3Info = new Mp3Info();
				long id = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media._ID)); // 閿熸枻鎷烽敓鏂ゆ嫹id
				String title = cursor.getString((cursor
						.getColumnIndex(MediaStore.Audio.Media.TITLE))); // 閿熸枻鎷烽敓琛楁唻鎷烽敓鏂ゆ嫹
				String artist = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷�
				long duration = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.DURATION)); // 鏃堕敓鏂ゆ嫹
				long size = cursor.getLong(cursor
						.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 閿熶茎纭锋嫹閿熸枻鎷峰皬
				String url = cursor.getString(cursor
						.getColumnIndex(MediaStore.Audio.Media.DATA)); // 閿熶茎纭锋嫹璺敓鏂ゆ嫹
				int isMusic = cursor.getInt(cursor
						.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 閿熻鍑ゆ嫹涓洪敓鏂ゆ嫹閿熸枻鎷�
				if (isMusic != 0) { // 鍙敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷锋嫢閿熸枻鎷烽敓鏂ゆ嫹绯婚敓鏂ゆ嫹閿燂拷
					mp3Info.setId(id);
					mp3Info.setTitle(title);
					mp3Info.setArtist(artist);
					mp3Info.setDuration(duration);
					mp3Info.setSize(size);
					mp3Info.setUrl(url);
					mp3Infos.add(mp3Info);
				}
			}
			return mp3Infos;
		}
	}

	/**
	 * 
	 */
	public void startPlay() {
		//
		
		try {
			Intent intent = new Intent();
			intent.setAction("com.music.player.PLAY_SERVICE");
			MusicPlayer.MUSCI_MSG = 1;
			isPlaying = true;
			context.startService(intent);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	public void play() {
		
		try {
			Intent intent = new Intent();
			if (isPlaying) {
				intent.setAction("com.music.player.PLAY_SERVICE");
				context.startService(intent);
				isPlaying = false;
				isPause = true;
				MusicPlayer.MUSCI_MSG = 2;

			} else if (isPause) {
				MusicPlayer.MUSCI_MSG = 4;
				intent.setAction("com.music.player.PLAY_SERVICE");
				context.startService(intent);
				isPause = false;
				isPlaying = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	/**
	 * 涓婁竴棣�
	 */
	public void previous_music() {
		MusicPlayer.position = MusicPlayer.position - 1;
		if (MusicPlayerActivity.isOnline == true
				&& MusicPlayer.position <= MusicPlayerActivity.mOlineMusicList
						.size() - 1) {
			if (MusicPlayer.position >= 0) {
				Intent intent = new Intent();
				MusicPlayer.MUSCI_MSG = 5;
				intent.setAction("com.music.player.PLAY_SERVICE");
				context.startService(intent);
			}
		} else {
			if (MusicPlayer.position >= 0) {
				Intent intent = new Intent();
				MusicPlayer.MUSCI_MSG = 5;
				intent.setAction("com.music.player.PLAY_SERVICE");
				context.startService(intent);
			}
		}

	}

	/**
	 * 涓嬩竴棣�
	 */
	public void next_music() {
		MusicPlayer.position = MusicPlayer.position + 1;
		if (MusicPlayerActivity.isOnline == true) {
			
			if(MusicPlayer.position <= MusicPlayerActivity.mOlineMusicList
					.size() - 1)
			{
				MusicPlayer.MUSCI_MSG = 6;
				Intent intent = new Intent();
				intent.setAction("com.music.player.PLAY_SERVICE");
				context.startService(intent);
			}
			else {
				MusicPlayer.position--;
			}
		} else {
			if (MusicPlayer.position <= MusicPlayer.musicInfos.size() - 1) {
				MusicPlayer.MUSCI_MSG = 6;
				Intent intent = new Intent();
				intent.setAction("com.music.player.PLAY_SERVICE");
				context.startService(intent);
			}
			else {
				MusicPlayer.position--;
			}
		}
	}

	/**
	 * 
	 */
	public void repeat_music() {
		// 閿熸枻鎷烽敓鏂ゆ嫹寰敓鏂ゆ嫹
		if (!(MusicPlayer.PLAY_ORDER == 3)) {
			MusicPlayer.PLAY_ORDER = 3;
		}
		// 鍏ㄩ敓鏂ゆ嫹寰敓鏂ゆ嫹
		else if (MusicPlayer.PLAY_ORDER == 3) {
			MusicPlayer.PLAY_ORDER = 4;
		}
	}

	/**
	 * 閿熸枻鎷烽敓鏂ゆ嫹 //0 椤洪敓鏂ゆ嫹 1 閿熸枻鎷烽敓锟�3 閿熸枻鎷烽敓鏂ゆ嫹
	 */
	public void shuffle_music() {
		if ((MusicPlayer.PLAY_ORDER == 0)) { // 閿熸枻鎷烽敓瑙掔櫧鍒侯儵顏庢嫹閿熸枻鎷烽敓鏂ゆ嫹鐗涢敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閽箣閿熸枻鎷疯皨閿熼樁鍒侯儵顏庢嫹閿熸枻鎷烽敓锟�
			MusicPlayer.PLAY_ORDER = 1;

		} else if (MusicPlayer.PLAY_ORDER == 1) {
			MusicPlayer.PLAY_ORDER = 3;
		} else if (MusicPlayer.PLAY_ORDER == 3) {
			MusicPlayer.PLAY_ORDER = 0;
		}
	}

	/**
	 * 閿熸枻鎷峰紡閿熸枻鎷锋椂閿熸垝锛岄敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹杞敓鏂ゆ嫹涓洪敓鏂ゆ嫹:閿熸枻鎷烽敓缁烇拷
	 * 
	 */
	public static String formatTime(long time) {
		String min = time / (1000 * 60) + "";
		String sec = time % (1000 * 60) + "";
		if (min.length() < 2) {
			min = "0" + time / (1000 * 60) + "";
		} else {
			min = time / (1000 * 60) + "";
		}
		if (sec.length() == 4) {
			sec = "0" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 3) {
			sec = "00" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 2) {
			sec = "000" + (time % (1000 * 60)) + "";
		} else if (sec.length() == 1) {
			sec = "0000" + (time % (1000 * 60)) + "";
		}
		return min + ":" + sec.trim().substring(0, 2);
	}

}
